import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';
import 'Toast.dart';

class Loading extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: double.infinity,
      child: Stack(
        alignment: Alignment.center,
        children: <Widget>[
          Container(
            width: 50,
            height: 50,
            child: CircularProgressIndicator(),
          )
        ],
      ),
    );
  }
}

class LoadingDialog extends CupertinoAlertDialog {
  const LoadingDialog(this.dismiss);

  final Function dismiss;

  @override
  Widget build(BuildContext context) {
    return _LoadingDialog(dismiss);
  }
}

class _LoadingDialog extends StatefulWidget {
  const _LoadingDialog(this.dismiss);

  final Function dismiss;

  @override
  _LoadingDialogState createState() => _LoadingDialogState(dismiss);
}

class _LoadingDialogState extends State<_LoadingDialog> {
  _LoadingDialogState(this.dismiss);

  Function dismiss;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Stack(
      alignment: Alignment.center,
      children: <Widget>[
        Container(
            width: 80,
            height: 80,
            padding: EdgeInsets.all(12),
            child: CircularProgressIndicator(),
            decoration: BoxDecoration(
                color: CupertinoColors.white,
                borderRadius: BorderRadius.circular(5),
                boxShadow: [
                  BoxShadow(
                      color: Color(0x0d212630),
                      blurRadius: 5,
                      offset: Offset(0, 1))
                ]))
      ],
    );
  }
}

Future showLoadingDialog(context, {Function dismiss}) async {
  return await showBgDialog(context,
      color: Color(0x00ffffff), barrierDismissible: false, builder: (context) {
    return LoadingDialog(() {
      if (dismiss != null) {
        dismiss();
      }
    });
  });
}

Future dismissLoadingDialog(context) async {
  await Navigator.of(context, rootNavigator: true).maybePop();
}

ToastFuture toast(String msg) {
  return showToastWidget(
    ToastWidget(
      msg: msg,
    ),
  );
}

Future showBgDialog(context,
    {@required WidgetBuilder builder,
    Color color = const Color(0x6604040F),
    bool barrierDismissible = true}) async {
  return await showGeneralDialog(
    context: context,
    barrierDismissible: barrierDismissible,
    barrierColor: color,
    transitionDuration: const Duration(milliseconds: 300),
    pageBuilder: (BuildContext context, Animation<double> animation,
        Animation<double> secondaryAnimation) {
      return builder(context);
    },
    transitionBuilder: _buildCupertinoDialogTransitions,
  );
}

Widget _buildCupertinoDialogTransitions(
    BuildContext context,
    Animation<double> animation,
    Animation<double> secondaryAnimation,
    Widget child) {
  final CurvedAnimation fadeAnimation = CurvedAnimation(
    parent: animation,
    curve: Curves.easeInOut,
  );
  if (animation.status == AnimationStatus.reverse) {
    return FadeTransition(
      opacity: fadeAnimation,
      child: child,
    );
  }
  return FadeTransition(
    opacity: fadeAnimation,
    child: ScaleTransition(
      child: child,
      scale: animation.drive(_dialogTween),
    ),
  );
}

final Animatable<double> _dialogTween = Tween<double>(begin: 1.3, end: 1.0)
    .chain(CurveTween(curve: Curves.linearToEaseOut));
